if exists (SELECT * from dbo.sysobjects WHERE id = object_id(N'[dbo].[vw_purchases_orders_labeled_distributed_amounts_totals]') AND OBJECTPROPERTY(id, N'IsView') = 1)
drop view [dbo].[vw_purchases_orders_labeled_distributed_amounts_totals]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

/***********************************************************************
* vw Name: dbo.vw_purchases_orders_labeled_distributed_amounts_totals
* Created: 20101130, RGM
* Purpose: listado de los totales distribuidos de lo etiquetado de una 
*	PO por planta - mes	y lo distribuido - etiquetado para esa misma 
*	planta de los meses anteriores
*
* Modified: yyyymmdd, user, data
*
* HistoryDate.
* yyyymmdd User, data
* 
* Note: nothing
* 
***********************************************************************/
CREATE VIEW dbo.vw_purchases_orders_labeled_distributed_amounts_totals AS
SELECT poa.purchase_order_version_id, poa.plant_version_id, poa.distributed_qty, 
		SUM(poa.old_distributed_qty) old_distributed_qty, 
		poa.distributed_qty + SUM(poa.old_distributed_qty) total_distributed_qty
	FROM vw_purchases_orders_labeled_distributed_amounts poa
	GROUP BY poa.purchase_order_version_id, poa.plant_version_id, poa.distributed_qty
	HAVING poa.distributed_qty > 0 
		OR SUM(poa.old_distributed_qty) > 0

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

